<template>
  <loginHeader>联合登录</loginHeader>
  <section class="container" v-if="isBind">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section class="container" v-else>
    <nav class="tab">
      <a
        @click="hasAccount = true"
        :class="{ active: hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        @click="hasAccount = false"
        :class="{ active: !hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :unionId="unionId"/>
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch :unionId="unionId"/>
    </div>
  </section>
  <LoginFooter></LoginFooter>
</template>

<script>
import loginHeader from './components/login-header.vue'
import LoginFooter from './components/login-footer'
import CallbackBind from './components/callback-bind.vue'
import CallbackPatch from './components/callback-patch.vue'
import Message from "@/components/library/message"
import { ref } from 'vue'
import { userQQLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

// import QC from "@/assets/js/QC"
export default {
  components: {
    loginHeader,
    LoginFooter,
    CallbackBind,
    CallbackPatch
  },
  name: 'LoginCallback',
  setup () {
    const router = useRouter()
    const store = useStore()
    const hasAccount = ref(true)
    const isBind = ref(true)
    const unionId = ref(null)
    // 首先：默认是已经注册且绑定
    // 通过qq的api获取openId就是接口需要的unionid然后登录
    // 成功：登录成功
    // 失败：qq号没有和小兔先账号绑定（有账号未绑定QQ，没账号未绑定qq）
    if (QC.Login.check()) {
      console.log(111)
      // QC.Login.check()确保qq已经登录
      QC.Login.getMe((openId) => {
        console.log(openId)
        unionId.value = openId
        // openId qq号
        userQQLogin(openId).then((data) => {
          console.log(data)
          // 登录成功
          // 存储用户信息
          console.log(1111111111)
          const { id, avatar, nickname, account, mobile, token } = data.result
          store.commit('user/setUser', { id, avatar, nickname, account, mobile, token })
          store.dispatch('car/mergCar').then(()=>{
               router.push(store.state.user.redirectUrl)
          Message({ type: 'success', text: 'QQ登录成功' })
          })
       
        })
          .catch((err) => {
            console.log(999999)
            // 登录失败
            isBind.value = false
          })
      })
    }
    return { hasAccount, isBind, unionId }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 25px 0;
    position: relative;
  height: 730px;
   .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../assets/images/load.gif) no-repeat center / 100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
